const http = require('../../utils/http.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    orderList: [],
    forMatStartTime: [],
    forMatEndTime: '',
    unconfirmed: [], //待确认
    confirmed: [], //已确认
    complete: [], //已完成
    reasonShow: false,
    reasonArr: [],
    reason_id: '',
    order_id: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function() {
    //获取取消理由
    http.getData("reason").then(res => {
      this.setData({
        reasonArr: res
      })
    }).catch(res => {
      console.log(res)
    })
  },
  onShow: function() {
    this.setData({
      forMatStartTime: [],
      forMatEndTime: '',
      unconfirmed: [], //待确认
      confirmed: [], //已确认
      complete: [] //已完成
    })
    let forMatStartTime = this.data.forMatStartTime;
    let unconfirmed = this.data.unconfirmed;
    let confirmed = this.data.confirmed;
    let complete = this.data.complete;
    http.getData("order/list").then(res => {
      // console.log(res)
      res.forEach((v, index) => {
        forMatStartTime.push(v.start_time.split(' ')[0])
        if (v.status == 0) {
          if (v.pay_status == 1 && v.downpayment > 0) {
            unconfirmed.push(v)
          } else if (v.pay_status == 0 && v.downpayment == 0) {
            unconfirmed.push(v)
          }
        } else if (v.status == 1) {
          confirmed.push(v)
        } else if (v.status == 2) {
          complete.push(v)
        }
      })
      this.setData({
        orderList: res,
        forMatStartTime: forMatStartTime,
        unconfirmed: unconfirmed,
        confirmed: confirmed,
        complete: complete
      })
      // console.log(this.data.unconfirmed)
      // console.log(this.data.confirmed)
      // console.log(this.data.complete)
    }).catch(res => {
      console.log(res)
    })
  },
  goOrderDetails(e) {
    let order_id = e.currentTarget.dataset.id
    let status = e.currentTarget.dataset.status
    wx.navigateTo({
      url: '/pages/orderDetails/orderDetails?order_id=' + order_id + '&status=' + status,
    })
  },
  swiperTab: function(e) {
    let that = this;
    that.setData({
      currentTab: e.detail.current
    })
  },
  clickTab: function(e) {
    let that = this;
    if (that.data.currentTab == e.currentTarget.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.currentTarget.dataset.current
      })
    }
  },
  cancelOrder(e) {
    this.setData({
      reasonShow: true,
      order_id: e.currentTarget.dataset.orderid,
    })
  },

  //取消订单原因选择
  radioChange(e) {
    let reason_id = e.detail.value;
    this.setData({
      reason_id: reason_id
    })
  },
  //确认
  choiceConfirm() {
    let that = this;
    if (that.data.reason_id == '') {
      wx.showToast({
        title: '请选择取消原因',
        icon: 'none',
        image: '/images/tanhao.png'
      })
    } else {
      wx.request({
        url: 'https://api.mokyun.com/pesticide/del/order',
        method: 'POST',
        header: {
          'Accept': 'application/json',
          'Authorization': 'Bearer ' + wx.getStorageSync('token'),
          'content-type': 'application/json',
          'APPID': 32
        },
        data: {
          id: that.data.order_id,
          reason_id: that.data.reason_id
        },
        success: (res) => {
          // console.log(res)
          if (res.data == 'success') {
            wx.showToast({
              title: '取消成功',
              success: (res) => {
                setTimeout(() => {
                  that.setData({
                    reasonShow: false
                  })
                  if (getCurrentPages().length != 0) {
                    //刷新当前页面的数据
                    getCurrentPages()[getCurrentPages().length - 1].onShow()
                  }
                }, 2000)
              }
            })
          }
        }
      })
    }
  },
  //取消
  choiceCancel() {
    this.setData({
      reasonShow: false
    })
  },
  goPay(e) {
    // console.log(e)
    let tradeNo = e.currentTarget.dataset.tradeno
    wx.request({
      url: 'https://api.mokyun.com/wechat/pay',
      method: 'POST',
      data: {
        tradeNo: tradeNo
      },
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token'),
        'APPID': 32
      },
      success: function(res) {
        // console.log(res)
        wx.requestPayment({
          timeStamp: res.data.timeStamp,
          nonceStr: res.data.nonceStr,
          package: res.data.package,
          signType: res.data.signType,
          paySign: res.data.paySign,
          success: function(res) {
            wx.request({
              url: "https://api.mokyun.com/pesticide/order/status",
              method: 'POST',
              data: {
                tradeNo: tradeNo
              },
              header: {
                'Authorization': 'Bearer ' + wx.getStorageSync('token'),
                'APPID': 32
              },
              success: function(res) {
                // console.log(res)
                if (res.data == 'success') {
                  wx.showToast({
                    title: '发布成功',
                    icon: 'none',
                    duration: 1500,
                  })
                  setTimeout(() => {
                    if (getCurrentPages().length != 0) {
                      //刷新当前页面的数据
                      getCurrentPages()[getCurrentPages().length - 1].onShow()
                    }
                  }, 2000)
                }
              }
            })
          },
          fail: function() {
            wx.showToast({
              title: '支付失败',
              icon: 'none',
              duration: 1500
            })
          }
        })
      }
    })
  }
})